﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    ConnectionData cn = new ConnectionData();
    protected void Page_Load(object sender, EventArgs e)
    {
        LayTinTuc();
        LayTinTucCu();
        LayTinTuc2();
        LayTinTuc3();
        LayDanhSachLoaiTinRao();
        LayDanhSachThanhPho();
        LayDanhSachLoaiDiaOc();
    }
    protected void btndangnhap_Click(object sender, EventArgs e)
    {

    }
    public void LayTinTuc()
    {
        try
        {
            String sql;
            
            sql = "select top 1 * from tin_tuc order by id desc";

            DataSet ds = new DataSet();
            ds = cn.ExecuteQuery(sql);
            dtlTintuc.DataSource = ds;
            dtlTintuc.DataBind();
        }
        catch (Exception e)
        {
            cn.Disconnect();
            
        }
    }

    
    protected void dtlTintuc_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        //tin moi nhat
        Image img = default(Image);
        img = (Image)e.Item.FindControl("imgHinh");
        img.ImageUrl = "HinhDiaOc/" + Convert.ToString(((DataRowView)e.Item.DataItem)["Hinh_Anh"]);
        

        /*Label tieude = default(Label);
        tieude = (Label)e.Item.FindControl("lbltieude");
        tieude.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Tieu_de"]);
        */

        HyperLink tieude = default(HyperLink);
        tieude = (HyperLink)e.Item.FindControl("hpltieude");
        tieude.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Tieu_de"]);
        tieude.NavigateUrl = "Chitiettintuc?id=" + Convert.ToString(((DataRowView)e.Item.DataItem)["id"]); 
        
        Label ndtt = default(Label);
        ndtt = (Label)e.Item.FindControl("lblNoidungtomtat");
        String nd = Convert.ToString(((DataRowView)e.Item.DataItem)["Mo_Ta"]);
        ndtt.Text = nd.Substring(0, 300) + " ...";
        //ket thuc tin moi nhat

        

    }

    public void LayTinTucCu()
    {
        String sql;
        sql = "select top 2 dt.tieu_de, dt.mo_ta, dt.Id, do.Hinh_anh from dang_tin as dt, Dia_Oc as do where dt.dia_oc_id=do.Id and Loai_Tin_ID = 1 order by id desc";

        DataSet ds = new DataSet();
        ds = cn.ExecuteQuery(sql);
        dtltintuccon.DataSource = ds;
        dtltintuccon.DataBind();
    }

    public void DataList(object sender, DataListItemEventArgs e)
    {
        //tin tuc con
        Image img = default(Image);
        img = (Image)e.Item.FindControl("imgHinh");
        img.ImageUrl = "HinhDiaOc/" + Convert.ToString(((DataRowView)e.Item.DataItem)["Hinh_Anh"]);

        HyperLink tieude = default(HyperLink);
        tieude = (HyperLink)e.Item.FindControl("hplTieudecon");
        tieude.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Tieu_de"]);
        tieude.NavigateUrl = "Chitiettintuc?id=" + Convert.ToString(((DataRowView)e.Item.DataItem)["id"]);

        Label ndtt = default(Label);
        ndtt = (Label)e.Item.FindControl("lblNoidungcon");
   
        ndtt.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Mo_Ta"]);
        //ket thuc tin tuc con
    }
    protected void dtltintuccon_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        //tin tuc con
        DataList(sender,e);
        //ket thuc tin tuc con
    }

    public void LayTinTuc2()
    {
        String sql;
        sql = "select top 2 dt.tieu_de, dt.mo_ta, dt.Id, do.Hinh_anh from dang_tin as dt, Dia_Oc as do where dt.dia_oc_id=do.Id and Loai_Tin_ID = 2 order by id desc";

        DataSet ds = new DataSet();
        ds = cn.ExecuteQuery(sql);
        dtltintuc2.DataSource = ds;
        dtltintuc2.DataBind();
    }

    protected void dtltintuc2_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        //tin tuc con
        DataList(sender, e);
        //ket thuc tin tuc con
    }

    public void LayTinTuc3()
    {
        String sql;
        sql = "select top 2 dt.tieu_de, dt.mo_ta, dt.Id, do.Hinh_anh from dang_tin as dt, Dia_Oc as do where dt.dia_oc_id=do.Id and Loai_Tin_ID = 3 or Loai_Tin_Id = 4 order by id desc";

        DataSet ds = new DataSet();
        ds = cn.ExecuteQuery(sql);
        dtltintuc3.DataSource = ds;
        dtltintuc3.DataBind();
    }

    protected void dtltintuc3_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        //tin tuc con
        DataList(sender, e);
        //ket thuc tin tuc con
    }

    //lay danh muc loai tin rao
    protected void LayDanhSachLoaiTinRao()
    {
        try
        {

            String sql = "select * from loai_tin";
            DataSet ds = new DataSet();
            cbloaitinrao.DataTextField = "Ten_Loai_tin";
            cbloaitinrao.DataValueField = "Id";
            cbloaitinrao.DataSource = cn.ExecuteQuery(sql).Tables[0];
            cbloaitinrao.DataBind();
            cbloaitinrao.SelectedValue = "1";


        }
        catch (Exception ex)
        {
            cn.Disconnect();
            cbloaitinrao.Enabled = false;
        }
    }
    //lay danh sach cac thanh pho
    protected void LayDanhSachThanhPho()
    {
        try
        {
            String sql = "select * from Thanh_Pho";
            DataSet ds = new DataSet();
            cbxthanhpho.DataTextField = "Ten_TP";
            cbxthanhpho.DataValueField = "Id";
            cbxthanhpho.DataSource = cn.ExecuteQuery(sql).Tables[0];
            //cbxthanhpho.DataSourceID = "Id";
            cbxthanhpho.SelectedValue = "1";
            cbxthanhpho.DataBind();
        }
        catch (Exception ex)
        {
            cn.Disconnect();

        }
    }

    // lay danh sách loai dia oc
    protected void LayDanhSachLoaiDiaOc()
    {
        try
        {
            String sql = "select * from loai_dia_oc";

            cbxLoaiDiaOc.DataTextField = "Ten_Loai_Dia_Oc";
            cbxLoaiDiaOc.DataValueField = "Id";
            cbxLoaiDiaOc.DataSource = cn.ExecuteQuery(sql).Tables[0];
            //cbxthanhpho.DataSourceID = "Id";
            cbxLoaiDiaOc.SelectedValue = "1";
            cbxLoaiDiaOc.DataBind();

        }
        catch (Exception ex)
        {
            cn.Disconnect();
        }
    }
    protected void btntimkiem_Click(object sender, EventArgs e)
    {
        Response.Redirect("Dangnhap.aspx");
    }
}
